import { fileURLToPath, URL } from 'node:url'
import Components from 'unplugin-vue-components/vite'
import { VantResolver } from 'unplugin-vue-components/resolvers'
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'
import path from 'path'
import { createHtmlPlugin } from 'vite-plugin-html'
import { viteMockServe } from 'vite-plugin-mock'
// https://vitejs.dev/config/
export default defineConfig({
  base: '/',
  server: {
    port: 80,
    host: '0.0.0.0',
    open: true,
    proxy: {
      '/api': {
        target: 'http://101.35.54.230:3366/',//这是你要跨域请求的地址前缀
        changeOrigin: true,//开启跨域
        rewrite: path => path.replace(/^\/api/, ''),//去除前缀api
      },
    },
  },
  plugins: [
    vue({
      reactivityTransform: true
    }),
    viteMockServe({
      mockPath: './src/mock',
      localEnabled: true
    }),
    createHtmlPlugin(),
    // 自动导入的插件，解析器可以是 vant element and-vue
    Components({
      dts: false,
      // 原因：Toast Confirm 这类组件的样式还是需要单独引入，样式全局引入了，关闭自动引入
      resolvers: [VantResolver({ importStyle: false })]
    }),
    createSvgIconsPlugin({
      // 指定图标文件夹，绝对路径（NODE代码）
      iconDirs: [path.resolve(process.cwd(), 'src/icons')]
    })
  ],
  resolve: {
    alias: {
      '@': fileURLToPath(new URL('./src', import.meta.url))
    }
  },
  css: {
    preprocessorOptions: {
      // 全局样式引入
      scss: {
        additionalData: '@import "./src/css/global.scss";',
        javascriptEnabled: true,
      },
    },
  },
  build: {
    rollupOptions: {
      output: {
        entryFileNames: 'js/[hash].js',
        chunkFileNames: 'js/[hash].js',
        assetFileNames(assetInfo) {
          if (assetInfo.name?.endsWith(".css")) {
            return 'css/[hash].css'
          }
          return 'acssets/[hash].[ext]';
        }
      }
    }
  }
})
